GBML manual / Lists

list_fill

Fills target right up to its capacity, with every place holding value. Afterwards list_length(target) is the same as its capacity, so target[i] works for every place from 0 up to the last one. Anything that was in target is replaced. A list made with list_create starts empty, so this is the way to fill it.

Syntax

list_fill(target, value)

Arguments

NameType
targetlist
valueelement

Returns

Nothing.

Description

Fills target, a list, right up to its capacity, so that every place in it holds value. Afterwards list_length(target) is the same number as the capacity, and target[0], target[1] and so on up to the last place all work.

A list made with list_create starts empty, with no places yet. list_fill is how you give it all of them at once.

It fills to the capacity, not just what is there

list_fill makes the list full. A list that holds 12 at most ends up with 12 items, every one of them value.

This is on purpose. A new list has nothing in it, so a fill that only changed the items already there would do nothing at all. And a list only has a place for an item that has been added: reading or writing items[5] on a list that holds three items stops the game with an "Invalid index" message. After list_fill, every place up to the capacity is there, so a loop like for (var i = 0; i < 12; i++) { items[i] = i; } is safe to run.

It is the list's version of what array_create does for an array: the whole thing starts out holding the same value.

What happens in the awkward cases

You do thisWhat happens
fill a list that is emptyevery place is added, and holds value
fill a list that already has itemsthey are all replaced. Items that were already there become value too, and the list grows to its capacity. Nothing from before survives
fill a list that is already fullit stays the same length, and every item becomes value
give it a value of the wrong kind, like list_fill(names, 5) for a list of textthe project is refused when it is checked, before the game runs, and the message says what kind the list holds
give it an array or a gridrefused when checked. An array or a grid is already full, so there is nothing to fill
a list with a capacity of 0cannot be made. A list's capacity is at least 1

If you only want to change what is there, without growing the list, use a loop with [i] instead, up to list_length. If you want to keep the items already in the list, add to it with list_add instead.

value is copied before anything is changed, so list_fill(items, items[0]) works: every place ends up holding what items[0] was a moment ago.

In a script

A script that takes a list as a parameter works on the caller's own list (see Lists). So list_fill inside a script fills the caller's list, up to the caller's capacity. A list that was made with room for 4 is filled with 4, and one made with room for 40 is filled with 40, whichever one the script was handed.

Example

var names = list_create(3, "");      // an empty list of text, room for 3
list_fill(names, "HI");              // names is now HI, HI, HI
var how_many = list_length(names);   // 3
names[2] = "BYE";                    // place 2 exists now, so this is fine

A list that already had something in it:

var scores = list_create(4, 0);
list_add(scores, 10);                // 10
list_fill(scores, 7);                // 7, 7, 7, 7 - the 10 was replaced too

On the GBA

The vector's assign(max_size(), value) - target ends up with exactly N items, N being its own declared capacity (`/// @capacity`, or 8 if untagged). Items that were already there are destroyed and replaced, so the result never depends on what target held before. O(N). value is copied before anything is cleared, so list_fill(items, items[0]) is safe. A list handed to a script is a reference to the capacity-free vector base, and its max_size() reports the capacity of the list that was really passed in, so inside a script this fills the CALLER's list to the CALLER's capacity, whatever that is. It never asserts: N is always a size the vector can hold.

See also: list_createlist_create_filllist_lengthlist_clearlist_addListsarray_createData containers